package cn.dankal.flyaudio.presenter;

import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.annotation.NonNull;

import java.util.List;

import api.ShopUserServiceFactory;
import cn.dankal.basiclib.common.bus.LiveDataBusConstants;
import cn.dankal.basiclib.pojo.AddressEntity;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.flyaudio.configs.LiveDataBusConstant;
import cn.dankal.flyaudio.contract.AddressManagerContract;

public class AddressManagerPresenter implements AddressManagerContract.Presenter {

    private AddressManagerContract.View mView;

    @Override
    public void attachView(@NonNull AddressManagerContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }

    @Override
    public void getMyAddress() {
        ShopUserServiceFactory.getUserAddress().subscribe(new AbstractSubscriber<BaseResponse<List<AddressEntity>>>(mView) {
            @Override
            public void onNext(BaseResponse<List<AddressEntity>> listBaseResponse) {
                mView.handleAddressList(listBaseResponse.getData());
            }
        });
    }

    @Override
    public void addAddress(AddressEntity params) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.addUserAddress(params).subscribe(new AbstractSubscriber<BaseResponse<AddressEntity>>(mView) {
            @Override
            public void onNext(BaseResponse<AddressEntity> addressEntityBaseResponse) {
                mView.dismissLoadingDialog();
                LiveEventBus.get(LiveDataBusConstants.ADD_ADDRESS_SUCCESS, String.class).post("");
                mView.finishActivity();

            }

        });
    }

    @Override
    public void UpdateAddress(AddressEntity params, boolean isMain) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.putUserAddress(params).subscribe(new AbstractSubscriber<BaseResponse<AddressEntity>>(mView) {
            @Override
            public void onNext(BaseResponse<AddressEntity> addressEntityBaseResponse) {
                mView.dismissLoadingDialog();
                LiveEventBus.get(LiveDataBusConstants.ADD_ADDRESS_SUCCESS, String.class).post("");
                if (isMain) {
//                    ToastUtils.showShort("已设置为默认地址");
                } else {
                    mView.finishActivity();
                }

            }
        });
    }

    @Override
    public void deleteAddress(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.deleteAddress(uuid).subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
            @Override
            public void onNext(BaseResponse<Object> addressEntityBaseResponse) {
                mView.dismissLoadingDialog();
                LiveEventBus.get(LiveDataBusConstant.DELETE_SHOPPING_CART_ADDRESS, String.class).post(uuid);
                getMyAddress();
            }
        });
    }
}
